home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-16  |  6.7 KB  |  275 lines

  1. #ifdef DEBUG
  2.  
  3. #include <exec/types.h>
  4. #include "defs.h"
  5.  
  6. static char rcsid [] = "$Id: debug.c,v 3.6 95/12/16 18:36:45 Martin_Apel Exp $";
  7.  
  8. /* Double-buffered */
  9. PRIVATE ULONG MemListEntries [2] [2000];
  10. PRIVATE ULONG CurrentBuffer;
  11.  
  12. PRIVATE struct DebugBuffer *DebugBuffer;
  13.  
  14. /******************************************************************/
  15.  
  16. BOOL OpenDebugWindow (void)
  17.  
  18. {
  19. ULONG debug_start;
  20. struct MemHeader *mh;
  21. BOOL success = FALSE;
  22. int i;
  23. ULONG *VectorTable;
  24. extern ULONG OrigFuncs [];
  25.  
  26. Forbid ();
  27.  
  28. for (mh = (struct MemHeader*) SysBase->MemList.lh_TailPred;
  29.      !success && (mh->mh_Node.ln_Pred != NULL); 
  30.      mh = (struct MemHeader*) mh->mh_Node.ln_Pred)
  31.   {
  32.   debug_start = (ULONG) mh->mh_Upper - sizeof (struct DebugBuffer);
  33.   DebugBuffer = AllocAbs (sizeof (struct DebugBuffer), (APTR)debug_start);
  34.   if (DebugBuffer != NULL)
  35.     success = TRUE;
  36.   }
  37.  
  38. Permit ();
  39. if (!success)
  40.   {
  41.   DebugBuffer = AllocMem (sizeof (struct DebugBuffer), MEMF_PUBLIC | MEMF_REVERSE);
  42.   if (DebugBuffer == NULL)
  43.     return (FALSE);
  44.   }
  45.  
  46. DebugBuffer->Magic1 = DEBUG_MAGIC1;
  47. DebugBuffer->Magic2 = DEBUG_MAGIC2;
  48. DebugBuffer->ThisBuffer = DebugBuffer;
  49. DebugBuffer->NextFree = DebugBuffer->DbgInfo;
  50.  
  51. /* Install crash handler */
  52.  
  53. Disable ();
  54. VectorTable = (ULONG*)ReadVBR ();
  55.  
  56. for (i = 3; i < 8; i++)
  57.   {
  58.   OrigFuncs [i] = *(VectorTable + i);
  59.   *(VectorTable + i) = (ULONG)&CrashHandler;
  60.   }
  61.  
  62. for (i = 9; i < 16; i++)
  63.   {
  64.   if (i != 11)
  65.     {
  66.     OrigFuncs [i] = *(VectorTable + i);
  67.     *(VectorTable + i) = (ULONG)&CrashHandler;
  68.     }
  69.   }
  70.  
  71. for (i = 48; i < 59; i++)
  72.   {
  73.   OrigFuncs [i] = *(VectorTable + i);
  74.   *(VectorTable + i) = (ULONG)&CrashHandler;
  75.   }
  76.  
  77. Enable ();
  78.  
  79. OrigAlert = (void (*) ()) SetFunction ((struct Library*)SysBase,
  80.                                    -0x6c, (ULONG (*) ()) AlertPatch);
  81.  
  82. return (TRUE);
  83. }
  84.  
  85. /******************************************************************/
  86.  
  87. void CloseDebugWindow (void)
  88.  
  89. {
  90. SetFunction ((struct Library*)SysBase, -0x6c, (ULONG (*) ()) OrigAlert);
  91.  
  92. if (DebugBuffer != NULL)
  93.   {
  94.   FreeMem (DebugBuffer, sizeof (struct DebugBuffer));
  95.   DebugBuffer = NULL;
  96.   }
  97. }
  98.  
  99. /******************************************************************/
  100.  
  101. #define SR_SV 0x2000
  102.  
  103. void PrintDebugMsg (char *string, long val)
  104.  
  105. {
  106. int length;
  107. char *Free;
  108. struct Task *ThisTask;
  109. struct Process *ThisProcess;
  110. char *name,
  111.      *name_filepart;
  112. BOOL SVMode;
  113. char tmp_string [100];
  114. char *my_string;
  115.  
  116. if (DebugBuffer == NULL)
  117.   return;
  118.  
  119. /* Be very careful. The string might be in virtual memory and must not
  120.  * be accessed then.
  121.  */
  122.  
  123. SVMode = SetSR (0L, 0L) & SR_SV;
  124.  
  125. ThisTask = SysBase->ThisTask;
  126. ThisProcess = (struct Process*)ThisTask;
  127.  
  128. if ((ThisTask->tc_Node.ln_Type == NT_PROCESS) && (ThisProcess->pr_CLI != NULL) &&
  129.   (((struct CommandLineInterface*)BADDR(ThisProcess->pr_CLI))->cli_CommandName != NULL))
  130.   {
  131.   name = (char*)BADDR(((struct CommandLineInterface*)
  132.                           BADDR(ThisProcess->pr_CLI))->cli_CommandName) + 1;
  133.   if (*name == 0)
  134.     {
  135.     name = ThisTask->tc_Node.ln_Name;
  136.     name_filepart = name;
  137.     }
  138.   else if (SVMode)
  139.     name_filepart = name;
  140.   else
  141.     name_filepart = FilePart (name);
  142.   }
  143. else
  144.   {
  145.   name = ThisTask->tc_Node.ln_Name;
  146.   name_filepart = name;
  147.   }
  148.  
  149. length = strlen (name_filepart);
  150.  
  151. if ((ULONG)string >= VirtAddrStart && (ULONG)string < VirtAddrEnd)
  152.   {
  153.   strcpy (tmp_string, string);
  154.   my_string = tmp_string;
  155.   }
  156. else
  157.   my_string = string;
  158.  
  159. length += strlen (my_string) + 2;          /* for ": " */
  160. if (SVMode)
  161.   length += 3;                          /* for "SV " */
  162.  
  163. Disable ();
  164.  
  165. if (DebugBuffer->NextFree + length + 5 >= (char*)&(DebugBuffer->Magic2))
  166.   DebugBuffer->NextFree = DebugBuffer->DbgInfo;    /* wrap around */
  167.  
  168. Free = DebugBuffer->NextFree;
  169.  
  170. if (SVMode)
  171.   {
  172.   strcpy (Free, "SV ");
  173.   strcat (Free, name_filepart);
  174.   }
  175. else
  176.   strcpy (Free, name_filepart);
  177.  
  178. strcat (Free, ": ");
  179. strcat (Free, my_string);
  180. Free += length + 1;
  181. if ((ULONG)Free & 1)
  182.   Free += 1;
  183.  
  184. *((long*)Free) = val;
  185. Free += 4;
  186. DebugBuffer->NextFree = Free;
  187. Enable ();
  188. if (CPushP != NULL)           /* This is called very early before the */
  189.   (*CPushP) ((ULONG)Free);    /* processor type has been determined */
  190. }
  191.  
  192. /******************************************************************/
  193.  
  194. void PrintTrapStruct (struct TrapStruct *ThisFault)
  195.  
  196. {
  197. ULONG *SP;
  198.  
  199. PRINT_DEB ("PrintTrapStruct: FaultTask = %lx", (ULONG)ThisFault->FaultTask);
  200. PRINT_DEB ("PrintTrapStruct: FaultAddr = %lx", ThisFault->FaultAddress);
  201. PRINT_DEB ("PrintTrapStruct: TopOfStack = %lx", (ULONG)ThisFault->TopOfStackFrame);
  202. PRINT_DEB ("PrintTrapStruct: WakeupSignal = %lx", (ULONG)ThisFault->WakeupSignal);
  203. SP = (ULONG*)&(ThisFault->TmpStack[TMP_STACKSIZE-16]);
  204. PRINT_DEB ("PrintTrapStruct: 4 lowest longwords from TmpStack", 0L);
  205. PRINT_DEB ("PrintTrapStruct:   %lx", *SP++);
  206. PRINT_DEB ("PrintTrapStruct:   %lx", *SP++);
  207. PRINT_DEB ("PrintTrapStruct:   %lx", *SP++);
  208. PRINT_DEB ("PrintTrapStruct:   %lx", *SP);
  209. PRINT_DEB ("PrintTrapStruct: Addr of page descr: %lx", (ULONG)ThisFault->PageDescrAddr);
  210. PRINT_DEB ("PrintTrapStruct: Page Descr = %lx", *(ThisFault->PageDescrAddr));
  211. PRINT_DEB ("PrintTrapStruct: PhysAddr = %lx", ThisFault->PhysAddr);
  212. }
  213.  
  214. /******************************************************************/
  215.  
  216. void CheckMemList (void)
  217.  
  218. {
  219. struct MemHeader *mh;
  220. struct MemChunk *mc;
  221. ULONG size_sum;
  222. ULONG num_entries;
  223. ULONG *tmp;
  224.  
  225. Disable ();
  226. for (mh = (struct MemHeader*)SysBase->MemList.lh_Head;
  227.      mh->mh_Node.ln_Succ != NULL;
  228.      mh = (struct MemHeader*)mh->mh_Node.ln_Succ)
  229.   {
  230.   if ((mh->mh_Attributes & (MEMF_PUBLIC | MEMF_FAST)) == (MEMF_PUBLIC | MEMF_FAST))
  231.     {
  232.     /* check only this one */
  233.     size_sum = 0L;
  234.     tmp = &(MemListEntries [CurrentBuffer] [0]);
  235.     for (mc = mh->mh_First; mc != NULL; mc = mc->mc_Next)
  236.       {
  237.       size_sum += mc->mc_Bytes;
  238.       *tmp++ = (ULONG)mc;
  239.       *tmp++ = mc->mc_Bytes;
  240.       }
  241.     *tmp = 0;
  242.     CurrentBuffer = (CurrentBuffer + 1) & 1;      /* switch buffer */
  243.     if (mh->mh_Free != size_sum)
  244.       {
  245.       PrintDebugMsg ("CHECK CONSISTENCY FAILED. MEMSIZE IN HEADER: %lx", mh->mh_Free);
  246.       PrintDebugMsg ("                          MEMSIZE IN LIST  : %lx", size_sum);
  247.       num_entries = 0;
  248.       for (mc = mh->mh_First; mc != NULL; mc = mc->mc_Next)
  249.         {
  250.         PrintDebugMsg ("Free chunk at %lx", (ULONG)mc);
  251.         PrintDebugMsg ("size %lx", mc->mc_Bytes);
  252.         num_entries++;
  253.         }
  254.       PRINT_DEB ("MemList has %ld entries", num_entries);
  255.       PRINT_DEB ("Current SP = %lx", (ULONG)&mh);
  256.       PRINT_DEB ("MemList before inconsistency:", 0L);
  257.       tmp = &(MemListEntries [CurrentBuffer] [0]);
  258.       while (*tmp != 0)
  259.         {
  260.         PRINT_DEB ("Free chunk at %lx", *tmp++);
  261.         PRINT_DEB ("size %lx", *tmp++);
  262.         }
  263.       ColdReboot ();
  264.       }
  265.     Enable ();
  266.     return;
  267.     }
  268.   }
  269. PrintDebugMsg ("CHECKMEMLIST: MEMHEADER NOT FOUND", 0L);
  270. ColdReboot ();
  271. Enable ();
  272. }
  273.  
  274. #endif
  275.